package XZK010302AND003.dao;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import XZK010302AND003.bean.Express;

/**
 * 快递E站-存储快递信息的操作类
 *
 * @author bing.z 2021/5/31 20:40
 **/
public class ExpressDao {
    /**
     * 集合类存储数据，模拟快递柜
     */
    private Map<String, Express> expressesList;

    {
        /**
         * 初始化快递柜
         */
        expressesList = new HashMap<>();
    }

    /**
     * 获取所有快递信息
     *
     * @return
     */
    public Map<String, Express> getAll() {
        return expressesList;
    }

    /**
     * 新增快递信息
     *
     * @param express
     * @return
     */
    public boolean insert(Express express) {
        expressesList.put(express.getNumber(), express);
        return true;
    }

    public int getSize() {
        return expressesList.size();
    }

    /**
     * 根据快递单号删除快递
     *
     * @param number
     * @return
     */
    public boolean delete(String number) {
        if (expressesList == null || expressesList.size() == 0) {
            return false;
        }
        Express express = expressesList.remove(number);
        return express == null ? false : true;
    }

    /**
     * 根据传入对象的快递单号，更新快递信息
     *
     * @param express
     * @return
     */
    public Express update(Express express) {
        Express put = expressesList.put(express.getNumber(), express);
        return put;
    }

    /**
     * 根据取件码获取对象
     *
     * @param code
     * @return
     */
    public Express getByCode(int code) {
        if (expressesList == null || expressesList.size() == 0) {
            return null;
        }
        Set<Map.Entry<String, Express>> entries = expressesList.entrySet();
        for (Map.Entry<String, Express> entry : entries) {
            Express express = entry.getValue();
            if (express.getCode() == code) {
                return express;
            }
        }
        return null;
    }

    /**
     * 根据快递单号获取快递对象
     *
     * @param number
     * @return
     */
    public Express getByNumber(String number) {
        if (expressesList == null || expressesList.size() == 0) {
            return null;
        }
        return expressesList.get(number);
    }
}
